package com.lmx.web.controller.thirdPart;

import com.lmx.persistence.service.LocalHbeaconService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;

/*-
 * 处理接口
 */

@Controller
@RequestMapping("/access/localhbeacon")
public class LocalHbeaconController {
	private static Log logger = LogFactory.getLog(LocalHbeaconController.class);

	@Autowired
	private LocalHbeaconService localHbeaconService;

	@RequestMapping("/deviceData")
	@ResponseBody
	public void hbeaconData(HttpServletRequest request, HttpServletResponse response) {
		// 数据处理
		String result = "";
		logger.debug("-----------------------------获取数据接口-------------------------------------");
		String inputLine;
		String dataJson = "";
		// dataJson ="{\"type\":\"swipe\",\"eventId\":\"2d0b42f9-1c93-40a4-8baa-64b70e9e0b7c\",\"data\":[{\"scanner\":\"feffaac89c58\",\"advertiser\":[{\"mac\":\"fe2bbd1985c7\",\"index\":\"5802\",\"timestamp\":1489402625851,\"message\":{\"buttonPressed\":true,\"buttonReleased\":false}}]}]}";
		// dataJson="{\"type\":\"swipe\",\"eventId\":\"e175490e-c037-4d51-a22f-4dd9774fea8b\",\"data\":[{\"scanner\":\"feffaac89c58\",\"advertiser\":[{\"mac\":\"c10f4c35bdda\",\"index\":\"5743\",\"timestamp\":1489402622288,\"message\":{\"stepCount\":0}},{\"mac\":\"c1c4c7973ec9\",\"index\":\"5722\",\"timestamp\":1489402621398,\"message\":{\"stepCount\":4780}},{\"mac\":\"c32e7be3883d\",\"index\":\"5713\",\"timestamp\":1489402620726,\"message\":{\"stepCount\":935}},{\"mac\":\"c3ad1da14275\",\"index\":\"5777\",\"timestamp\":1489402624429,\"message\":{\"stepCount\":0}},{\"mac\":\"c5647e3fe6be\",\"index\":\"5703\",\"timestamp\":1489402620070,\"message\":{\"stepCount\":0}},{\"mac\":\"d3597243b5ef\",\"index\":\"5766\",\"timestamp\":1489402623898,\"message\":{\"stepCount\":0}},{\"mac\":\"e0d09c803a91\",\"index\":\"5748\",\"timestamp\":1489402622710,\"message\":{\"stepCount\":0}},{\"mac\":\"ea7b39e02209\",\"index\":\"5701\",\"timestamp\":1489402619648,\"message\":{\"stepCount\":0}},{\"mac\":\"eb65502460c5\",\"index\":\"5735\",\"timestamp\":1489402621945,\"message\":{\"stepCount\":0}},{\"mac\":\"f13e8271d3fa\",\"index\":\"5716\",\"timestamp\":1489402620945,\"message\":{\"stepCount\":0}},{\"mac\":\"fb479a0daf33\",\"index\":\"5847\",\"timestamp\":1489402628507,\"message\":{\"stepCount\":0}},{\"mac\":\"fe2bbd1985c7\",\"index\":\"5719\",\"timestamp\":1489402621179,\"message\":{\"stepCount\":0}}]}]}";
		// guihua
		// dataJson="{\"type\":\"calling\",\"id\":1,\"data\":[{\"scanner\":\"cc1be0e0a7e9\",\"advertiser\":[{\"mac\":\"cc1be0e80a9a\",\"type\":1,\"timestamp\":1500289226}]}],\"version\":1499948624}";
		// dataJson="{\"type\":\"location\",\"data\":[{\"scanner\":\"cc1be0e0d9cc\",\"advertiser\":[]},{\"scanner\":\"cc1be0e0d81c\",\"advertiser\":[]},{\"scanner\":\"cc1be0e0d6fc\",\"advertiser\":[]},{\"scanner\":\"cc1be0e0c3cc\",\"advertiser\":[]},{\"scanner\":\"cc1be0e0a7e3\",\"advertiser\":[]},{\"scanner\":\"cc1be0e0a7e9\",\"advertiser\":[{\"mac\":\"cc1be0e80a9a\",\"timestamp\":1500290350,\"deviceType\":\"band\",\"battery\":71}]}]}";
		try {
			while ((inputLine = request.getReader().readLine()) != null) {
				dataJson += inputLine;
			}
			logger.debug("-------------------hbeaconData: " + dataJson);
			if (dataJson != null && !"".equals(dataJson)) {
				localHbeaconService.addDeviceInfo(dataJson);
			}
			request.getReader().close();
			BufferedOutputStream out;
			out = new BufferedOutputStream(response.getOutputStream());
			out.write("http ok".getBytes());
			out.flush();
			out.close();
		} catch (Exception e) {
			logger.debug("receive device error:" + e);
		}
	}

	@RequestMapping("/scannerData")
	@ResponseBody
	public void scannerData(HttpServletRequest request, HttpServletResponse response) {
		// 数据处理
		String result = "";
		logger.debug("-----------------------------获取数据接口-------------------------------------");
		String inputLine;
		String dataJson = "";
		//dataJson="{\"status\":\"online\",\"timestamp\":1500289082,\"scanner\":[\"CC:1B:E0:E0:A7:E9\",\"CC:1B:E0:E0:A7:E3\"]}";
		try {
			while ((inputLine = request.getReader().readLine()) != null) {
				dataJson += inputLine;
			}
			logger.debug("-------------------scannerData: " + dataJson);
			// dataJson="["+dataJson+"]";
			if (dataJson != null && !"".equals(dataJson)) {
				localHbeaconService.receiveScannerStatus(dataJson);
			}
			request.getReader().close();
			BufferedOutputStream out;
			out = new BufferedOutputStream(response.getOutputStream());
			out.write("http ok".getBytes());
			out.flush();
			out.close();
		} catch (Exception e) {
			logger.debug("receive scanner error:" + e);
		}
	}

	@RequestMapping("/callingResult")
	@ResponseBody
	public void callingResult(HttpServletRequest request, HttpServletResponse response) {
		// 数据处理
		String result = "";
		logger.debug("-----------------------------获取数据接口-------------------------------------");
		String inputLine;
		String dataJson = "";
		try {
			while ((inputLine = request.getReader().readLine()) != null) {
				dataJson += inputLine;
			}
			logger.debug("-------------------callingResult: " + dataJson);
			if (dataJson != null && !"".equals(dataJson)) {
				localHbeaconService.receiveCallingResult(dataJson);
			}
			request.getReader().close();
			BufferedOutputStream out;
			out = new BufferedOutputStream(response.getOutputStream());
			out.write("http ok".getBytes());
			out.flush();
			out.close();
		} catch (Exception e) {
			logger.debug("receive callingResult error:" + e);
		}
	}
}
